package com.rraj.common.mq.test.messagelistener;

import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.order.ConsumeOrderContext;
import com.aliyun.openservices.ons.api.order.MessageOrderListener;
import com.aliyun.openservices.ons.api.order.OrderAction;
import com.xiaoleilu.hutool.date.DateUtil;
import org.springframework.stereotype.Component;

/**
 * Created by WangSheng on 2017/6/8.
 * 全局顺序消费
 *
 * @author WangSheng
 * @date 2017/06/08
 */
@Component
public class MessageOrderListenerImpl implements MessageOrderListener {
    /**
     * 1. 消息消费处理失败或者处理出现异常，返回OrderAction.Suspend<br>
     * 2. 消息处理成功，返回与返回OrderAction.Success
     */
    @Override
    public OrderAction consume(Message message, ConsumeOrderContext context) {
        try {
            System.out.println(DateUtil.now() + " 收到全局顺序消费消息, Topic is:" +
                message.getTopic() + ", MsgId is:" + message.getMsgID() + ",message body:" + new String(
                message.getBody(), "UTF-8"));
        } catch (Exception e) {
            return OrderAction.Suspend;
        }
        //如果想测试消息重投的功能,可以将Action.CommitMessage 替换成Action.ReconsumeLater
        return OrderAction.Success;
    }
}
